﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Red.Controls;
using Red.Controls.DataDriven;
using Red.Controls.Shell;
using Red.Controls.ViewModels;
using Red.Core;

namespace Red.Toolkit.MeshEditor
{
    public class dupa : RedTreeItemView
    {
        public dupa()
        { }
    }
    public partial class StructureNodesView : UserControl
    {
        public Canvas Icon { get; set; }

        public StructureNodesView()
        {
            InitializeComponent();
            DataContextChanged += OnDataContextChanged;
        }

        private void OnDataContextChanged( object sender, DependencyPropertyChangedEventArgs e )
        {
            InteropTreeItem treeItem = e.NewValue as InteropTreeItem;

            if ( treeItem == null )
                return;

            treeItem.Image = Icon;
        }
    }

    [InteropControl( "LodsNode", "LodNode", "ChunkNode", "ChunkMaterialNode" )]
    public class LodsNodesView : StructureNodesView
    {
        private static Canvas _icon;
        private static IEnumerable<RedCommand> _lodsCommands;
        private static IEnumerable<RedCommand> _lodCommands;

        static LodsNodesView()
        {
             _icon = RedApplication.FindCanvasResource( "appbar_layer" );
            _lodsCommands = new List<RedCommand>()
            {
                MeshStructureCommands.AddLOD,
                MeshStructureCommands.GenerateLODs
            };

            _lodCommands = new List<RedCommand>()
            {
                MeshStructureCommands.RemoveLOD
            };
        }

        public LodsNodesView()
        {
            Icon = _icon;
            DataContextChanged += LodsNodesView_DataContextChanged;
        }

        private void LodsNodesView_DataContextChanged( object sender, DependencyPropertyChangedEventArgs e )
        {
            InteropTreeItem treeItem = e.NewValue as InteropTreeItem;

            if ( treeItem == null )
                return;

            List<RedMenuDataItem> menu = new List<RedMenuDataItem>();
            if ( treeItem.Type == "LodsNode" )
            {
                menu = _lodsCommands.Select( command => new RedMenuDataItem( new CommandWrapper( command, treeItem ) ) ).ToList();
            }
            else if ( treeItem.Type == "LodNode" )
            {
                menu = _lodCommands.Select( command => new RedMenuDataItem( new CommandWrapper( command, treeItem ) ) ).ToList();
            }
            treeItem.SetMenu( menu );
        }
    }

    [InteropControl( "CollisionNode", "CollidersNode", "ColliderNode" )]
    public class CollidersNodesView : StructureNodesView
    {
        private static Canvas _icon;
        private static IEnumerable<RedMenuDataItem> _commands;

        static CollidersNodesView()
        {
            _icon = RedApplication.FindCanvasResource( "appbar_box" );
            _commands = new List<RedMenuDataItem>()
            {
                new RedMenuDataItem( new CommandWrapper( "Add box collision", MeshStructureCommands.AddCollider, "Box" ) ),
                new RedMenuDataItem( new CommandWrapper( "Add sphere collision", MeshStructureCommands.AddCollider, "Sphere" ) ),
                new RedMenuDataItem( new CommandWrapper( "Add capsule collision", MeshStructureCommands.AddCollider, "Capsule" ) ),
                new RedMenuDataItem( new CommandWrapper( "Add trimesh collision", MeshStructureCommands.AddCollider, "Trimesh" ) ),
                new RedMenuDataItem( new CommandWrapper( "Remove custom collision", MeshStructureCommands.RemoveCollider ) )
            };
        }

        public CollidersNodesView()
        {
            Icon = _icon;
            DataContextChanged += CollidersNodesView_DataContextChanged;
        }

        private void CollidersNodesView_DataContextChanged( object sender, DependencyPropertyChangedEventArgs e )
        {
            InteropTreeItem treeItem = e.NewValue as InteropTreeItem;

            if ( treeItem == null )
                return;

            if ( treeItem.Type == "CollisionNode" )
            {
                treeItem.SetMenu( _commands.ToList() );
            }
        }
    }
}
